package lc;

import java.util.Arrays;

import org.junit.Test;

public class Ex443 {
    class Solution {
        public int compress(char[] chars) {
            int res = 0, idx = 0, len = chars.length, cnt, i;
            while (idx < len) {
                cnt = 1;
                i = idx + 1;
                while (i < len && chars[i - 1] == chars[i]) {
                    cnt++;
                    i++;
                }

                chars[res++] = chars[idx];
                if (cnt > 1) {
                    int d = getMaxDigit(cnt);
                    res += d;
                    int j = res - 1;
                    while (cnt != 0) {
                        chars[j--] = (char)(cnt % 10 + '0');
                        cnt /= 10;
                    }
                } 
                idx = i;
            }
            return res;
        }

        public int getMaxDigit(int n) {
            int cnt = 0;
            while (n != 0) {
                cnt++;
                n /= 10;
            }
            return cnt;
        }
    }



    @Test
    public void test() {
        Solution s = new Solution();
        //["a","b","b","b","b","b","b","b","b","b","b","b","b"]
        char[] nums = new char[]{'a', 'a', 'b', 'b', 'c', 'c', 'c'};
        nums = new char[]{'a', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'};
        System.out.println(s.compress(nums));
        System.out.println(Arrays.toString(nums));
    }
}
